<template>
  <ele-modal
    form
    :width="1100"
    :model-value="modelValue"
    :title="title"
    @update:modelValue="updateModelValue"
  >
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        highlight-current-row
        v-model:selections="selections"
        border
        stripe
        row-key="id"
        row-click-checked
      >
        <template #img="{ row }">
          <span class="pointer blue" @click="openImages(row)">查看</span>
        </template>
      </ele-pro-table>
      <images v-model="showImages" :data="current" />
    </ele-card>
  </ele-modal>
</template>

<script setup>
  import { ref, watch, computed } from 'vue';
  import { pageBillRepayRecord } from '@/api/company';
  import Images from './images.vue';

  const emit = defineEmits(['done', 'update:modelValue']);

  const props = defineProps({
    /** 弹窗是否打开 */
    modelValue: Boolean,
    data: Object
  });
  const title = ref('');
  /** 表格实例 */
  const tableRef = ref(null);

  /** 表格列配置 */
  const columns = computed(() => {
    const cols = [];

    cols.push(
      {
        width: 100,
        prop: 'repay_type',
        label: '类型'
      },
      {
        minWidth: 100,
        prop: 'amount',
        label: '还款金额'
      },
      {
        width: 160,
        prop: 'pay_time',
        label: '还款时间'
      },
      {
        width: 120,
        prop: 'create_user',
        label: '操作人'
      },
      {
        width: 160,
        prop: 'create_time',
        label: '操作时间'
      },
      {
        width: 120,
        prop: 'judge_user',
        label: '审核'
      },
      {
        width: 160,
        prop: 'judge_time',
        label: '审核时间'
      },
      {
        width: 100,
        slot: 'img',
        label: '付款凭证'
      }
    );

    return cols;
  });

  const current = ref([]);

  /** 表格数据源 */

  const datasource = async ({ page, limit, where, orders }) => {
    const result = await pageBillRepayRecord({
      ...where,
      ...orders,
      page,
      limit,
      bill_id: props.data.id,
      status: 200
    });
    return result;
  };

  /** 更新modelValue */
  const updateModelValue = (value) => {
    emit('update:modelValue', value);
  };

  /** 查看图片 */
  const showImages = ref(false);
  const openImages = (data) => {
    current.value = data;
    showImages.value = true;
  };

  watch(
    () => props.modelValue,
    (modelValue) => {
      if (modelValue) {
        title.value =
          props.data.plate_number + '的' + props.data.cate + '还款记录';
      } else {
        console.log('关闭');
      }
    }
  );
</script>
